package com.example.questionnaires.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.questionnaires.dto.AnnouncementCommentDTO;
import com.example.questionnaires.entity.AnnouncementComment;
import com.example.questionnaires.entity.User;
import com.example.questionnaires.mapper.AnnouncementCommentMapper;
import com.example.questionnaires.mapper.UserMapper;
import com.example.questionnaires.service.AnnouncementCommentService;
import com.example.questionnaires.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AnnouncementCommentServiceImpl implements AnnouncementCommentService {
    
    @Resource
    private AnnouncementCommentMapper commentMapper;
    
    @Resource
    private UserMapper userMapper;

    @Override
    public List<AnnouncementCommentDTO> getCommentsByAnnouncementId(Long announcementId) {
        // 查询评论
        List<AnnouncementComment> comments = commentMapper.selectList(
            new LambdaQueryWrapper<AnnouncementComment>()
                .eq(AnnouncementComment::getAnnouncementId, announcementId)
                .orderByDesc(AnnouncementComment::getCreateTime)
        );
        
        // 如果没有评论，直接返回空列表
        if (comments.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取所有用户ID
        List<Long> userIds = comments.stream()
            .map(AnnouncementComment::getUserId)
            .distinct()
            .collect(Collectors.toList());
            
        // 查询用户信息
        Map<Long, String> userMap = userMapper.selectList(
            new LambdaQueryWrapper<User>()
                .in(User::getId, userIds)
        ).stream().collect(
            Collectors.toMap(User::getId, User::getUsername)
        );
        
        // 转换为DTO
        return comments.stream().map(comment -> {
            AnnouncementCommentDTO dto = new AnnouncementCommentDTO();
            BeanUtils.copyProperties(comment, dto);
            dto.setUsername(userMap.get(comment.getUserId()));
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void addComment(Long announcementId, String content) {
        User user = UserContext.getCurrentUser();
        if (user == null) {
            throw new RuntimeException("请先登录");
        }
        
        AnnouncementComment comment = new AnnouncementComment();
        comment.setAnnouncementId(announcementId);
        comment.setUserId(user.getId());
        comment.setContent(content);
        
        commentMapper.insert(comment);
    }

    @Override
    @Transactional
    public void deleteComment(Long commentId) {
        User user = UserContext.getCurrentUser();
        if (user == null) {
            throw new RuntimeException("请先登录");
        }
        
        // 只有管理员可以删除评论
        if (user.getRole() != 1) {
            throw new RuntimeException("无权限删除评论");
        }
        
        commentMapper.deleteById(commentId);
    }
} 